load "properties.rb"


def to_class(name)
	"#{name}PropertyPage"
end

def file_include
	includes = []
	PROPERTIES.each do |property|
		includes << "#include \"#{to_class(property[:identity])}.h\""
	end
	includes.join("\n")
end

def case_line
	cases = []
	id = 1
	PROPERTIES.each do |property|
		cases << "case PropertyTemplateId::#{property[:identity]}: return new #{to_class(property[:identity])}(tmpl);"
		id += 1
	end
	cases.join("\n\t\t\t")
end

def template_id
	ids = []
	id = 1
	PROPERTIES.each do |property|
		ids << "static const int #{property[:identity]} = #{id};"
		id += 1
	end
	ids.join("\n\t\t\t")
end

File.open('templates/property_template', 'r') do |template_file|
	out_file = File.new('out/PropertyPageFactory.cpp', 'w')
	while line = template_file.gets
		line.gsub!(/\$\(include\)/, file_include)
		line.gsub!(/\$\(case\)/, case_line)
		out_file.puts(line)
	end
	out_file.close
end

sql_file = File.new('out/property_templates.sql', 'w')
sql_file.puts("delete from property_templates;")
sql_file.puts("delete from item_type_property_mappings;")
id = 1
PROPERTIES.each do |property|
	sql = "INSERT INTO property_templates (`id`, `name`, `description`) VALUES (#{id}, '#{property[:name]}', '#{property[:desc]}');"
	sql_file.puts(sql)
	property[:item_types].each do |item_type|
		mappings = "INSERT INTO item_type_property_mappings (`property_id`, `item_type`) VALUES (#{id}, #{item_type});"
		sql_file.puts(mappings)
	end
	id += 1
end
sql_file.close

File.open('templates/property_template_id_template', 'r') do |template_file|
	out_file = File.new('out/PropertyTemplateId.h', 'w')
	while line = template_file.gets
		line.gsub!(/\$\(template_id\)/, template_id)
		out_file.puts(line)
	end
	out_file.close
end

file = File.new('out/ItemProperty.txt', 'w')
id = 1
PROPERTIES.each do |property|
	file.puts property[:name]
	file.puts property[:identity]
	file.puts "template_id = #{id}"
	if property.has_key?(:r1)
		file.puts "reserved1 = #{property[:r1]}"
	end
	if property.has_key?(:r2)
		file.puts "reserved2 = #{property[:r2]}"
	end
	if property.has_key?(:r3)
		file.puts "reserved3 = #{property[:r3]}"
	end
	if property.has_key?(:r4)
		file.puts "reserved4 = #{property[:r4]}"
	end
	file.puts
	id += 1
end
file.close
